import cv2

# 创建VideoCapture对象，参数为设备索引，通常0表示默认摄像头
cap = cv2.VideoCapture(0)

# 检查摄像头是否成功打开
if not cap.isOpened():
    print("Error: Could not open camera.")
    exit()

# 设置窗口大小
cv2.namedWindow('Camera', cv2.WINDOW_NORMAL)
cv2.resizeWindow('Camera', 100, 100)

# 循环读取摄像头捕获的视频
while True:
    # 读取一帧视频
    ret, frame = cap.read()

    # 检查是否成功读取
    if not ret:
        print("Error: Could not read frame.")
        break

    # 显示视频
    cv2.imshow('Camera', frame)

    # 按下'q'键退出循环
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# 释放摄像头并关闭窗口
cap.release()
cv2.destroyAllWindows()
